<?php

namespace app\api\controller;

use library\Controller;
use library\File as Files;

class File extends Controller
{
    /**
     * 上传图片
     */
    public function upload()
    {
        // 文件接收
        try {
            $file = $this->request->file('file');
        } catch (\Exception $e) {
            $this->error(lang($e->getMessage()));
        }
        if (!$file->checkExt(strtolower(admin_sysconf('storage_local_exts')))) {
            $this->error('文件上传类型受限！');
        }
        if ($file->checkExt('php')) {
            $this->error('可执行文件禁止上传到本地服务器！');
        }
        // 唯一名称
        $ext = strtolower(pathinfo($file->getInfo('name'), PATHINFO_EXTENSION));
        $name = md5_file($file->getPathname());
        $name = substr($name, 0, 2) . '/' . substr($name, 2) . '.' . $ext;
        $pathinfo = pathinfo(Files::instance('local')->path($name, false));
        if ($file->move($pathinfo['dirname'], $pathinfo['basename'], true)) {
            if (is_array($info = Files::instance('local')->info($name, false)) && isset($info['url'])) {
                $this->success('success！', ['site_url' => $info['url']]);
            }
        }
        $this->error('文件上传失败，请稍候再试！');
    }

    /**
     * 获取本地文件对象
     * @return \think\File
     */
    private function getUploadFile()
    {
        try {
            return $this->request->file('file');
        } catch (\Exception $e) {
            $this->error(lang($e->getMessage()));
        }
    }

    /**
     * Plupload 插件上传文件
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function ossupload()
    {
        if (!($file = $this->getUploadFile()) || empty($file)) {
            $this->error('文件上传异常，文件可能过大或未上传！');
        }
        if (!$file->checkExt(strtolower(admin_sysconf('storage_local_exts')))) {
            $this->error('文件上传类型受限，请在后台配置！');
        }
        if ($file->checkExt('php')) {
            $this->error('可执行文件禁止上传到本地服务器！');
        }

        $this->safe = $this->getUploadSafe();
        $this->uptype = $this->getUploadType();
        $this->ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);
//        if($this->ext == 'mp4'){
//            $this->uptype = 'local';
//        }
        $name = Files::name($file->getPathname(), $this->ext, '', 'md5_file');
        $info = Files::instance($this->uptype)->save($name, file_get_contents($file->getRealPath()), $this->safe);

        if (is_array($info) && isset($info['url'])) {
            $this->success('文件上传成功！', ['site_url' => $info['url']]);
        }

        $this->error('文件处理失败，请稍候再试！');

    }

    /**
     * 获取上传安全模式
     * @return boolean
     */
    private function getUploadSafe()
    {
        return $this->safe = boolval(input('safe'));
    }

    /**
     * 获取文件上传方式
     * @return string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    private function getUploadType()
    {
        $this->uptype = input('uptype');
        if (!in_array($this->uptype, ['local', 'oss', 'qiniu'])) {
            $this->uptype = admin_sysconf('storage_type');
        }
        return $this->uptype;
    }

}